package com.ruoyi.web.controller.liangshi;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.liangshi.domain.ArchItem;
import com.ruoyi.liangshi.domain.BizKcKsc;
import com.ruoyi.liangshi.domain.BsOrder;
import com.ruoyi.liangshi.domain.GdArchPoint;
import com.ruoyi.liangshi.service.*;
import com.ruoyi.liangshi.vo.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author LiuJin
 * @Date: Create in 2023-11-16 15:18
 * @Description: this class is
 */
@RestController
@RequestMapping("/liangshi/bs-order")
public class BsOrderController extends BaseController {

    @Autowired
    private IBsOrderService bsOrderService;

    @Autowired
    private IBizBsOrderService bizBsOrderService;
    @Autowired
    private IGdArchPointService gdArchPointService;

    @Autowired
    private IBizKcKscService kcKscService;

    @Autowired
    private IArchItemService atchItemService;
    @Value("${arch-item.excluds}")
    private String archItemExcluds;
    /**
     * 获取订单列表
     * @param orderQuery
     * @return
     */
    @GetMapping("/getOrderList")
    public TableDataInfo getOrderList(OrderQuery orderQuery){
        startPage();
        if(StringUtils.isEmpty(orderQuery.getBeginTime())){
            return null ;
        }
        orderQuery.setBeginTime(orderQuery.getBeginTime());
        orderQuery.setEndTime(orderQuery.getEndTime());
        List<BsOrder> list = bsOrderService.getOrderList(orderQuery);
        //获取座位数据
        List<GdArchPoint> pointList = gdArchPointService.list();
        //座位信息list转为map字典
        Map<Long, String> pointMaps = pointList.stream().collect(Collectors.toMap(GdArchPoint::getId, GdArchPoint::getName));
        for (BsOrder bsOrder : list) {
            String name = pointMaps.get(bsOrder.getPointId());
            if(StrUtil.isNotBlank(name)){
                bsOrder.setPointName(name);
            }
            //构造实体，菜品难易程度
            List<String> excludeItemIds = Arrays.asList(archItemExcluds.split(","));
            List<BizKcKsc> kcKscList = kcKscService.lambdaQuery().eq(BizKcKsc::getBsId, bsOrder.getId()).notIn(BizKcKsc::getItemId,excludeItemIds).list();
            Map<String, BigDecimal> atchTypeInfo=new HashMap<>();
            for (BizKcKsc kcKsc : kcKscList) {
                ArchItem atchItem = atchItemService.getById(kcKsc.getItemId());
                if(ObjectUtil.isNull(atchItem)){
                    continue;
                }
                BigDecimal bigDecimal = atchTypeInfo.get(atchItem.getAtchType());
                if(ObjectUtil.isNull(bigDecimal)){
                    atchTypeInfo.put(atchItem.getAtchType(),BigDecimal.ONE);
                }else {
                    bigDecimal=bigDecimal.add(BigDecimal.ONE);
                    atchTypeInfo.put(atchItem.getAtchType(),bigDecimal);
                }
            }
            bsOrder.setAtchTypeMaps(atchTypeInfo);
        }
        return getDataTable(list);
    }

    /**
     * 获取首页数据详情
     * @return
     */
    @GetMapping("/getBiBsOrderDashboardData")
    public AjaxResult getBiBsOrderDashboardData(){
        BiBsOrderDashboardDataVo biBsOrderDashboardData = bizBsOrderService.getBiBsOrderDashboardData();
        AjaxResult success = success();
        success.put("currentData",biBsOrderDashboardData);
        return success;
    }

    /**
     * 获取首页门店折线图数据
     * @param type
     * @return
     */
    @GetMapping("/getBiBsOrderDeptDashboardData")
    public AjaxResult getBiBsOrderDeptDashboardData(@RequestParam("type") String type){
        List<BiBsOrderDeptDashboardDataVo> biBsOrderDeptDashboardData = bizBsOrderService.getBiBsOrderDeptDashboardData(type);
        Map<String, List<BiBsOrderDeptDashboardDataVo>> listMap = biBsOrderDeptDashboardData.stream().collect(Collectors.groupingBy(BiBsOrderDeptDashboardDataVo::getDeptName));
        AjaxResult success = success();
        success.put("listMap",listMap);
        return success;
    }

    /**
     * 菜品情况列表
     * @param orderAndKscCommonQueryVo
     * @return
     */
    @GetMapping("/getKscDataList")
    public TableDataInfo getKscDataList(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo){
        startPage();
        List<BiBsKscDataVo> kscDataList = bizBsOrderService.getKscDataList(orderAndKscCommonQueryVo);
        return getDataTable(kscDataList);
    }
    /**
     * 订单整体情况
     * @param orderAndKscCommonQueryVo
     * @return
     */
    @GetMapping("/getOrderDataInfo")
    public AjaxResult getOrderDataInfo(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo){
        BiBsOrderDataInfoVo orderDataInfo = bizBsOrderService.getOrderDataInfo(orderAndKscCommonQueryVo);
        AjaxResult success = success();
        success.put("data",orderDataInfo);
        return success;
    }

    /**
     * 获取超时订单和菜品数量
     * @return
     */
    @GetMapping("/getTimeOutOrderAndKsc")
    public AjaxResult getTimeOutOrderAndKsc(OrderAndKscCommonQueryVo orderAndKscCommonQueryVo){
        BiBsTimeOutDataVo timeOutDishesData = kcKscService.getTimeOutDishesData(orderAndKscCommonQueryVo);
        Integer timeOutOrders = bizBsOrderService.getTimeOutOrders(orderAndKscCommonQueryVo);
        timeOutDishesData.setTimeOutOrders(timeOutOrders);
        AjaxResult success = success();
        success.put("data",timeOutDishesData);
        return success;
    }

    @PostMapping("/getTodayOrderList")
    public AjaxResult getTodayOrderList(){
        QueryWrapper<BizKcKsc>  wrapper = new QueryWrapper<>();
        wrapper.between("call_up_time",LocalDateTime.MIN.toString(),LocalDateTime.MAX.toString());
        wrapper.eq("kitchen_flg",3);
        wrapper.eq("kitchen_flg",3);
        List<BizKcKsc>  result =  kcKscService.list(wrapper);
        return  AjaxResult.success("请求成功",result) ;
    }

}
